﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Workflows
{
    public abstract class StageEventProcessor
    {

        internal void Process(WFStep step, EventScenario scenario)
        {
            if (step == null)
                throw new ArgumentNullException("step");

            if (step.StageName != Stage.Name || step.Thread.Process.WorkflowName != Stage.Workflow.Name ||
                !IsScenarioSupported(scenario))
                throw new WorkflowException("Cannot process the specified step.");

            ProcessCore(step, scenario);
        }

        protected internal abstract bool IsScenarioSupported(EventScenario scenario);

        protected abstract void ProcessCore(WFStep step, EventScenario scenario);
        
        private Stage _stage;

        public Stage Stage
        {
            get { return _stage; }
            internal set
            {
                if (_stage != value)
                {
                    _stage = value;
                    OnStageChanged();
                }
            }
        }

        protected virtual void OnStageChanged()
        {
        }

        /// <summary>
        /// Gets a value indicates whether this processor should exclusivly process event under one scenario.
        /// </summary>
        public virtual bool Exclusive
        {
            get
            {
                return true;
            }
        }
    }
}
